///////////////////////////////////////////////////////////////////////////////////////////////////
// Illusion Ray Development Kit (IDK)           Copyright (c) 2010 ( dimitriy.martynenko@gmail.com )
///////////////////////////////////////////////////////////////////////////////////////////////////
// Created : 2010-05-22
// Updated : ...
// Licence : NEW BSD
// Info	   : IDK Engine
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once

namespace Core
{
	/*-------------------------------------------------------------------------
	| BasicCamera:
	| ------------
	| BasicCamera
	---------------------------------------------------------------------------*/
	class TMaterial : public TStaticArray<ref<IDK::ITexture>, IDK::IMaterial::TextureMode::MAX>, virtual public IDK::IMaterial
	{
		typedef TStaticArray<ref<IDK::ITexture>, IDK::IMaterial::TextureMode::MAX> Base;
		IDKClass;
	private:
		ref<IDK::IShader> mShader;
		String mName;
		//
		IDK::IRenderState::Blend::Enum mBlend;
	public:
		TMaterial(const String &name) : Base(), 
			mName(name),
			mBlend(IDK::IRenderState::Blend::None)
		{
		}

		virtual void SetShader(ref<IDK::IShader> shader)
		{
			mShader = shader;
		}

		void SetBlendMode(IDK::IRenderState::Blend::Enum mode)
		{
			mBlend = mode;
		}

		virtual void SetTexture(TextureMode::Enum mode, ref<IDK::ITexture> tex)
		{
			mStore[mode] = tex;
		};

		virtual void Rendered(IDK::IRenderState& /* rndState*/)
		{

		}

		virtual bool Render(IDK::IRenderState& rndState)
		{
			mShader->Render(rndState);

			//apply states
			rndState.SetBlend(mBlend);

			//
			for(int i = TextureMode::MAX - 1; i>=TextureMode::Sampler0; --i)
			{
				if(mStore[i] == NullPtr)
				{
					continue;
				}
				mStore[i]->BindTexture(rndState, i);
			}
			return true;
		}

		virtual const String& GetName(void) const
		{
			return mName;
		}

		virtual void SetName(const String &name)
		{
			mName = name;
		}
	};
	typedef ref<TMaterial> Material;

}
